#include "stdafx.h"
#include "./tmTimer.h"

TimerProc sProc = NULL;
VOID*	  sUser = NULL;
LARGE_INTEGER  Freq;
GLES::Bool tmTimer::m_bUsingQPF;
GLES::Uns64 tmTimer::m_llQPFTickesPerSec = 1000;
GLES::Uns64 tmTimer::GetTicksPerSecond(){return tmTimer::m_llQPFTickesPerSec;}

VOID  CALLBACK   adpTimerProc( HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime)
{
	sProc(sUser);
	return;
}

BOOL tmTimer::CreateTimer( int timerID,TimerProc proc,int span ,VOID* pUser)
{
	if(mCreated)
		return true;
	myAdapData data;
	//data.proc = proc;
	//data.pUser = pUser;
	sProc = proc;
	sUser = pUser;

	int i = ::SetTimer(NULL,1,span,adpTimerProc);
	DWORD R = ::GetLastError();
	mTimerID = timerID;
	mCreated = true; 
	return true;
}

VOID tmTimer::DeleteTimer( int timerID )
{
	if(mCreated == false)
		return;

	KillTimer(NULL ,timerID);
	mCreated = false;
	return;
}

tmTimer::tmTimer():m_llCountedLen(0),m_llUncountedLen(0),m_llElapsedLen(0),mCreated(false)
{
	tmTimer::m_bUsingQPF	=(QueryPerformanceFrequency(&Freq)!=0);
	tmTimer::m_llQPFTickesPerSec=Freq.QuadPart;
	Reset(NULL);
}

tmTimer::~tmTimer()
{
	DeleteTimer(mTimerID);
}




VOID	 tmTimer::Reset(GLES::Uns64* ResetTime)
{
	m_llCountedLen=0;
	m_llUncountedLen=0;
	m_llElapsedLen=0;
	if(tmTimer::m_bUsingQPF)
	{
		LARGE_INTEGER qwTime;
		QueryPerformanceCounter(&qwTime);
		m_llLastStoppedTime		=	0;
		m_llLastResetedTime     =	qwTime.QuadPart;
		m_llLastElapsedTime		=	qwTime.QuadPart;
		if(ResetTime)*ResetTime=qwTime.QuadPart;
	}
}
VOID	 tmTimer::Start(GLES::Uns64* StartTime)
{
	if(tmTimer::m_bUsingQPF)
	{
		LARGE_INTEGER qwTime;
		QueryPerformanceCounter(&qwTime);
		if(m_llLastStoppedTime != 0)
		{
			m_llUncountedLen		+=qwTime.QuadPart-m_llLastStoppedTime;
			m_llLastElapsedTime		+=qwTime.QuadPart-m_llLastStoppedTime;
			m_llLastStoppedTime		=0;
		}
		if(StartTime)*StartTime=qwTime.QuadPart;
	}
}
VOID	 tmTimer::Stop(GLES::Uns64* StopTime)
{
	if(tmTimer::m_bUsingQPF)
	{
		if(m_llLastStoppedTime == 0)
		{
			LARGE_INTEGER qwTime;
			QueryPerformanceCounter(&qwTime);
			m_llLastStoppedTime		=qwTime.QuadPart;
			if(StopTime)*StopTime	=qwTime.QuadPart;
		}
	}
}
GLES::Uns64 tmTimer::GetTicksElapsed(){return m_llElapsedLen;}
GLES::Uns64 tmTimer::GetTicksCounted(){return m_llCountedLen;}
GLES::Uns64 tmTimer::CalTicksCounted()
{
	if(!tmTimer::m_bUsingQPF)return -1;
	LARGE_INTEGER qwTime;
	if(m_llLastStoppedTime !=0 )//stopped
		qwTime.QuadPart = m_llLastStoppedTime;
	else
		QueryPerformanceCounter(&qwTime);
	m_llCountedLen = qwTime.QuadPart - m_llUncountedLen - m_llLastResetedTime;
	return m_llCountedLen;
}
GLES::Uns64 tmTimer::CalTicksElapsed()
{
	if(!tmTimer::m_bUsingQPF)return -1;
	LARGE_INTEGER qwTime;
	if(m_llLastStoppedTime != 0)//stopped
		qwTime.QuadPart = m_llLastStoppedTime;
	else 
		QueryPerformanceCounter(&qwTime);
	m_llElapsedLen = qwTime.QuadPart - m_llLastElapsedTime;
	m_llLastElapsedTime = qwTime.QuadPart;
	return m_llElapsedLen;
}
GLES::Bool   tmTimer::IsStopped()
{
	return (m_llLastStoppedTime != 0);
}